-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   zxzf
-- @email:    07291@qq.com
-- @date:     2024/6/3
-- @module:   shop
-- @describe:
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class shop
local shop = {}
local this = shop

local npc_shop_unit = npc_shop_unit
local npc_shop_ctx = npc_shop_ctx
local cash_unit = cash_unit
local cash_ctx = cash_ctx

---@type shop_cfg
local shop_cfg = import("game/config/shop_cfg")

-- 得到道具
function this.get_npcShop_item_info_by_name(item_name, price, vague)
    -- 模糊查找
    vague = vague or false
    local item_info = nil
    local item_list = npc_shop_unit.cur_shop_item_list()
    -- 判断是否为模糊查询
    if vague then
        for i = 1, #item_list do
            -- 如果物品列表中有该物品名,且价格相同，则返回该物品列表
            if string.find(item_list[i].name, item_name) and item_list[i].price == price then
                item_info = item_list[i]
            end
        end
    else
        for i = 1, #item_list do
            -- 精准查找
            if item_list[i].name == item_name and item_list[i].price == price then
                item_info = item_list[i]
            end
        end
    end

    return item_info
end

-- 得到npc商店列表
function this.get_npcShop_item_list()
    local list = npc_shop_unit.list()
    local ret = {}
    for i = 1, #list do
        local obj = list[i]
        if npc_shop_ctx:init(obj) then
            table.insert(ret, {
                obj = obj,
                res_ptr = npc_shop_ctx:res_ptr(),
                id = npc_shop_ctx:id(),
                res_id = npc_shop_ctx:res_id(),
                type = npc_shop_ctx:type(),
                price = npc_shop_ctx:price(),
                --can_buy = npc_shop_ctx:can_buy(),
                name = npc_shop_ctx:name()
            })
        end
    end
    return ret
end

-- 通过名字得到需要购买的配置
function this.get_cash_configList_by_name(name)
    local cfg = shop_cfg.CASH_ITEM
    local ret = nil
    for k, v in pairs(cfg) do
        if string.find(k, name) then
            if not ret then
                ret = {}
            end
            table.insert(ret, v)
        end
    end
    return ret
end

-- 得到商店物品数据
function this.get_shop_itemInfo()
    local list = cash_unit.list()
    local ret = nil
    for i = 1, #list do
        local obj = list[i]
        if cash_ctx:init(obj) then
            if not ret then
                ret = {}
            end
            table.insert(ret, {
                obj = obj,
                res_ptr = cash_ctx:res_ptr(),
                id = cash_ctx:id(),
                money_type = cash_ctx:money_type(),
                price = cash_ctx:price(),
                cur_buy_num = cash_ctx:cur_buy_num(),
                max_buy_num = cash_ctx:max_buy_num(),
                name = cash_ctx:name()
            })
        end
    end
    return ret
end

return this
